<template>
  <view class="task-history">
    <!-- 任务信息 -->
    <view class="task-info" v-if="taskInfo">
      <text class="task-name">{{ taskInfo.task_name }}</text>
      <text class="task-description">{{ taskInfo.task_des }}</text>
    </view>

    <!-- 历史记录列表 -->
    <view class="history-list">
      <view 
        v-for="(record, index) in historyRecords" 
        :key="index" 
        class="record-item"
      >
        <view class="record-header">
          <text class="record-title">{{ record.project_name }}</text>
          <!-- <text class="record-type">{{ getTypeText(record.type) }}</text> -->
        </view>
        
        <view class="record-content">
          <view class="record-field">
            <text class="field-label">项目名称:</text>
            <text class="field-value">{{ record.project_name }}</text>
          </view>
          
          <view class="record-field">
            <text class="field-label">任务名称:</text>
            <text class="field-value">{{ record.task_name }}</text>
          </view>

          <view class="record-field">
            <text class="field-label">对应表类型·:</text>
            <text class="field-value">{{ record.type }}</text>
          </view>
          
          <!-- <view class="record-field">
            <text class="field-label">行记录索引:</text>
            <text class="field-value">{{ record.acc_name }}</text>
          </view> -->
          
          <view class="record-field">
            <text class="field-label">验收问题:</text>
            <text class="field-value">{{ record.acc_problem }}</text>
          </view>
          
          <view class="record-field">
            <text class="field-label">正确值:</text>
            <text class="field-value">{{ record.right_value }}</text>
          </view>
          
          <view class="record-field">
            <text class="field-label" v-if="['用量清单','机器使用','仪器使用'].includes(record.type)">定额编码:</text>
            <text class="field-label" v-else-if="['甲供材料','乙供材料'].includes(record.type)">材料名称:</text>
            <!-- <text class="field-label" v-else>字段名称:</text> -->
            <text class="field-value">{{ record.field_1 }}</text>
          </view>
          
          <view class="record-field">
            <text class="field-label" v-if="['用量清单','机器使用','仪器使用'].includes(record.type)">定额名称:</text>
            <text class="field-label" v-else-if="['甲供材料','乙供材料'].includes(record.type)">规格型号:</text>
            <text class="field-value">{{ record.field_2 }}</text>
          </view>
          
          <view class="record-field">
            <text class="field-label">数量:</text>
            <text class="field-value">{{ record.field_3 }}</text>
          </view>
          
          <view class="record-field">
            <text class="field-label">单位:</text>
            <text class="field-value">{{ record.field_4 }}</text>
          </view>
        </view>

        <!-- 图片展示 -->
        <view class="record-images" v-if="record.photo_path && record.photo_path.length > 0">
          <text class="images-title">佐证图片:</text>
          <view class="images-grid">
            <image 
              v-for="(photo, photoIndex) in record.photo_path" 
              :key="photoIndex"
              :src="photo.data" 
              class="record-image"
              mode="aspectFill"
              @click="previewImage(record.photo_path, photoIndex)"
            />
          </view>
        </view>
      </view>
    </view>

    <!-- 加载状态 -->
    <view class="loading" v-if="loading">
      <text>加载中...</text>
    </view>

    <!-- 空状态 -->
    <view class="empty" v-if="!loading && historyRecords.length === 0">
      <text>暂无历史记录</text>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { onLoad } from '@dcloudio/uni-app'

// 响应式数据
const taskId = ref('')
const taskInfo = ref(null)
const historyRecords = ref([])
const loading = ref(false)
const token = uni.getStorageSync("userInfo").token

// 页面加载
onLoad((options) => {
  if (options.task_id) {
    taskId.value = options.task_id
    loadTaskInfo()
    loadHistoryRecords()
  }
})

// 加载任务信息
const loadTaskInfo = async () => {
  try {
    const response = await uni.request({
      url: './api/acc_task',
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Authorization': `Bearer ${token}`
      }
    })
    
    if (response.statusCode === 200 && response.data) {
      const tasks = response.data
      taskInfo.value = tasks.find(task => task.task_id === taskId.value)
    }
  } catch (error) {
    console.error('获取任务信息失败:', error)
  }
}

// 加载历史记录
const loadHistoryRecords = async () => {
  loading.value = true
  try {
    const response = await uni.request({
      url: './api/GetRecordDone',
      method: 'POST',
      data: {
        record_id: taskId.value
      },
      header: {
        'content-type': 'application/json',
        'Authorization': `Bearer ${token}`
      }
    })
    
    if (response.statusCode === 200) {
      historyRecords.value = response.data || []
    } else {
      uni.showToast({
        title: '获取历史记录失败',
        icon: 'none'
      })
    }
  } catch (error) {
    console.error('获取历史记录失败:', error)
    uni.showToast({
      title: '网络错误',
      icon: 'none'
    })
  } finally {
    loading.value = false
  }
}

// 获取类型文本
const getTypeText = (type) => {
  const typeMap = {
    'usage': '使用记录',
    'jg': 'JG材料',
    'yg': 'YG材料',
    'machine': '机器使用',
    'instrument': '仪器使用'
  }
  return typeMap[type] || type
}

// 图片预览
const previewImage = (images, currentIndex) => {
  const urls = images.map(img => img.data)
  uni.previewImage({
    urls: urls,
    current: currentIndex
  })
}

// 返回上一页
const goBack = () => {
  uni.navigateBack()
}
</script>

<style scoped>
.task-history {
  min-height: 100vh;
  background: #f5f7fa;
  padding-bottom: 40rpx;
}

/* 头部导航 */
.header {
  background: white;
  padding: 40rpx 30rpx;
  display: flex;
  align-items: center;
  border-bottom: 2rpx solid #e8ecf0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-back {
  display: flex;
  align-items: center;
  margin-right: 30rpx;
}

.back-icon {
  font-size: 40rpx;
  margin-right: 10rpx;
  color: #3498ff;
}

.back-text {
  font-size: 32rpx;
  color: #3498ff;
}

.header-title {
  font-size: 36rpx;
  font-weight: 700;
  color: #2c3e50;
  flex: 1;
  text-align: center;
  margin-right: 100rpx;
}

/* 任务信息 */
.task-info {
  background: white;
  margin: 20rpx;
  padding: 40rpx 30rpx;
  border-radius: 20rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
}

.task-name {
  font-size: 38rpx;
  font-weight: 700;
  color: #2c3e50;
  display: block;
  margin-bottom: 20rpx;
}

.task-description {
  font-size: 30rpx;
  color: #6c757d;
  display: block;
  line-height: 1.6;
}

/* 历史记录列表 */
.history-list {
  padding: 20rpx;
}

.record-item {
  background: white;
  margin-bottom: 30rpx;
  border-radius: 20rpx;
  padding: 40rpx 30rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
  border: 2rpx solid #f8f9fa;
}

.record-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30rpx;
  padding-bottom: 20rpx;
  border-bottom: 2rpx solid #f8f9fa;
}

.record-title {
  font-size: 34rpx;
  font-weight: 600;
  color: #2c3e50;
}

.record-type {
  font-size: 30rpx; color: white; background: linear-gradient(45deg, #3498db, #2980b9); 
  padding: 8rpx 20rpx; border-radius: 20rpx; 
  box-shadow: 0 4rpx 8rpx rgba(52, 152, 255, 0.3); transition: all 0.3s ease; text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.2);
}

.record-content {
  margin-bottom: 30rpx;
}

.record-field {
  display: flex;
  margin-bottom: 20rpx;
  align-items: flex-start;
}

.field-label {
  font-size: 30rpx;
  color: #6c757d;
  min-width: 140rpx;
  margin-right: 20rpx;
  font-weight: 500;
}

.field-value {
  font-size: 30rpx;
  color: #2c3e50;
  flex: 1;
  line-height: 1.5;
  word-break: break-all;
}

/* 图片展示 */
.record-images {
  margin-top: 30rpx;
  padding-top: 30rpx;
  border-top: 2rpx solid #f8f9fa;
}

.images-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #2c3e50;
  display: block;
  margin-bottom: 20rpx;
}

.images-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20rpx;
}

.record-image {
  width: 100%;
  height: 200rpx;
  border-radius: 12rpx;
  background: #f8f9fa;
}

/* 加载状态 */
.loading {
  text-align: center;
  padding: 100rpx 0;
  font-size: 32rpx;
  color: #6c757d;
}

/* 空状态 */
.empty {
  text-align: center;
  padding: 100rpx 0;
  font-size: 32rpx;
  color: #6c757d;
}

/* 响应式优化 */
@media (max-width: 750rpx) {
  .record-item {
    padding: 30rpx 25rpx;
  }
  
  .images-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .record-image {
    height: 180rpx;
  }
}
</style>